<?php

namespace App\Models;


class ProblemRecord extends BaseModel
{
    public static $instance;

    /**
     * 添加记录
     */
    public static function addNewRecord($type, $ids, $problem)
    {
        foreach ($ids as $id) {
            $info = ProblemRecord::query()
                ->where('type', 1)
                ->where('data_id', $id)
                ->where('problem', $problem)
                ->first();
            if (empty($info)) {
                $bookTitleInfo = BookTitle::query()->where('id', $id)
                    ->select('title', 'base_map', 'blueprint', 'word_material', 'volume', 'page_num', 'years', 'store_period', 'start_date', 'end_date')
                    ->first()
                    ->toArray();

                $user   = user();
                $object = new ProblemRecord();
                $object->type     = $type;
                $object->data_id  = $id;
                $object->problem  = $problem;
                $object->before   = json_encode($bookTitleInfo, JSON_UNESCAPED_UNICODE);
                $object->after    = '';
                $object->nickname = $user->nickname;
                $object->phone    = $user->phone;

                $object->save();
            }
        }
    }

    /**
     * 更新记录
     */
    public static function updateRecord($type, $id, $after)
    {
        $problemRecord = ProblemRecord::query()
            ->where('type', $type)
            ->where('data_id', $id)
            ->first();
        if ($problemRecord) {
            $problemRecord->after = $after;
            $problemRecord->save();
        }
    }

}
